package org.lottery.fragment;

import org.lottery.activity.R;
import org.lottery.activity.SettingsActivity;
import org.lottery.util.AlertUtil;
import org.lottery.util.ImageUtil;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.widget.ImageView;

public class SettingsFragment extends PreferenceFragment {

    private ImageView image;
    public static final int REWARD_PICTURE_LOADED = 1;
    public static final int GOODLUCK_PICTURE_LOADED = 2;
    public static final int OPEN_PICTURE_LOADED = 3;

    private SharedPreferences shared;
    private Editor edit;

    public SharedPreferences getShared() {

        return shared;
    }

    public void setShared(SharedPreferences shared) {

        edit = shared.edit();
        this.shared = shared;
    }

    public ImageView getImage() {

        return image;
    }

    public void setImage(ImageView image) {

        this.image = image;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        addEvent();

    }

    private void addEvent() {

        findPreference("settings_reward_quantity").setOnPreferenceChangeListener(
                new Preference.OnPreferenceChangeListener() {

                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {

                        return validRewardQuantity(newValue);
                    }

                });

        findPreference("settings_reward_path").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {

                openGallery(REWARD_PICTURE_LOADED);
                return true;
            }
        });

        findPreference("settings_goodluck_path").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {

                openGallery(GOODLUCK_PICTURE_LOADED);
                return true;
            }
        });
        findPreference("settings_open_path").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {

                openGallery(OPEN_PICTURE_LOADED);
                return true;
            }
        });
    }

    private boolean validRewardQuantity(Object newValue) {

        boolean valid = false;
        try {
            if (1 <= Integer.parseInt(newValue + "") && Integer.parseInt(newValue + "") <= 9) {
                valid = true;
            } else {
                AlertUtil.alert(getActivity(), "Error could not save setting, valid input is number from 1-9");
            }
        } catch (Exception e) {
            AlertUtil.alert(getActivity(), "Error could not save setting, valid input is number from 1-9");
        }
        if (valid)
            AlertUtil.alert(getActivity(), "Save successed");
        return valid;
    }

    protected void openGallery(int mode) {

        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, mode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == -1 && data != null) {
                String imagePath = ImageUtil.getPicturePath(data, getActivity().getContentResolver());
                switch (requestCode) {
                case REWARD_PICTURE_LOADED:
                    save(SettingsActivity.SETTINGS_REWARD_PATH, imagePath);
                    break;

                case GOODLUCK_PICTURE_LOADED:
                    save(SettingsActivity.SETTINGS_GOODLUCK_PATH, imagePath);
                    break;
                case OPEN_PICTURE_LOADED:
                    save(SettingsActivity.SETTINGS_OPEN_PATH, imagePath);
                    break;
                default:
                    break;
                }
                AlertUtil.alert(getActivity(), "Save successed");
            }
            if (requestCode == REWARD_PICTURE_LOADED && resultCode == -1 && data != null) {

            }
        } catch (Exception e) {
            System.out.println("ERROR onActivityResult ");
        }

    }

    private void save(String pref, String value) {

        edit.putString(pref, value);
        edit.apply();
        edit.commit();
    }
}
